package com.finger.crowd.controller;

import com.finger.crowd.api.MySQLRemoteService;
import com.finger.crowd.constant.CrowdConstant;
import com.finger.crowd.entity.vo.AddressVO;
import com.finger.crowd.entity.vo.LoginMemberVO;
import com.finger.crowd.entity.vo.OrderProjectVO;
import com.finger.crowd.util.ResultEntity;
import com.sun.xml.internal.ws.resources.HttpserverMessages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    private Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private MySQLRemoteService mySQLRemoteService;

    @RequestMapping("/return/confirm/info/{returnId}/{projectId}")
    public String reutrnConfirmInfo(@PathVariable("returnId") Integer returnId,
                                    @PathVariable("projectId") Integer projectId,
                                    Model model,
                                    HttpSession session) {

        ResultEntity<OrderProjectVO> getOrderProjectVOResultEntity = mySQLRemoteService.getOrderProjectVORemote(returnId, projectId);

        if (ResultEntity.getSUCCESS().equals(getOrderProjectVOResultEntity.getResult())) {

            OrderProjectVO orderProjectVO = getOrderProjectVOResultEntity.getData();

            model.addAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECTVO, orderProjectVO);

            // 存入session供后边使用
            session.setAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECTVO, orderProjectVO);
        }

        return "show-return-confirm";
    }

    @RequestMapping("/confirm/address/{returnCount}")
    public String getAddressList(@PathVariable("returnCount") Integer returnCount,
                                 HttpSession session, Model model) {
        // 从session域中取出orderprojectvo
        OrderProjectVO orderProjectVO
                = (OrderProjectVO) session.getAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECTVO);

        if (orderProjectVO != null) {

            // 更新回报数量
            orderProjectVO.setReturnCount(returnCount);

            // 一定要重新设置回去，上一步设置的仅在于内存中
            session.setAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECTVO, orderProjectVO);
        }

        // 从session中取出登录的用户
        LoginMemberVO loginMemberVO = (LoginMemberVO) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);

        if (loginMemberVO != null) {

            Integer memberId = loginMemberVO.getId();

            // 根据用户id得到 address集合
            ResultEntity<List<AddressVO>> getAddressResultEntity = mySQLRemoteService.getAllAddress(memberId);

            if (ResultEntity.getSUCCESS().equals(getAddressResultEntity.getResult())) {

                List<AddressVO> addressList = getAddressResultEntity.getData();

                model.addAttribute(CrowdConstant.ATTR_NAME_ADDRESS, addressList);
            }
        }

        return "order-confirm";
    }

    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO,
                              HttpSession session) {


        ResultEntity<String> saveAddressResultEntity =
                mySQLRemoteService.saveAddress(addressVO);

        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECTVO);

        Integer returnCount = orderProjectVO.getReturnCount();

        return "redirect:http://www.crowd.com/order/confirm/address/" + returnCount;

    }

}
